#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from model import QuestionM

class questionsHandler(webapp.RequestHandler):
    def post(self):
        lectureKey = self.request.get('key')
        QuestionM.resetQuestions(lectureKey)
        i = 1
        while(self.request.get('name' + str(i))):
            name = self.request.get('name' + str(i))
            rightAnswer = int(float(self.request.get('rightAnswer' + str(i))))
            answers = []
            j = 1
            while(self.request.get('answer' + str(i) + '_' + str(j))):
                answers.append(self.request.get('answer' + str(i) + '_' + str(j)))
                j += 1
            QuestionM.addQuestion(name, answers, rightAnswer, lectureKey, i)
            i += 1
            
application = webapp.WSGIApplication([(r'.*', questionsHandler)],debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == '__main__':
    main()